<template>
  <div>
    <MyHeader title="购物车案例"></MyHeader>
    <div class="main">
      <!-- 直接使用 都需要新增模块名 -->
      <MyGoods 
      v-for="(obj,index) in $store.state.cart.goodsList" 
      :key="obj.id"
      :i="index"
      :gObj="obj"
      ></MyGoods>
    </div>
    <MyFooter @changeAll="allFn" :arr="list"></MyFooter>
  </div>
</template>

<script>
import MyHeader from './components/MyHeader'
import MyGoods from './components/MyGoods'
import MyFooter from './components/MyFooter'

import { mapState, mapActions } from 'vuex'
export default {
  computed: {
    // 映射  都需要新增模块名
    ...mapState('cart', {
      list: 'goodsList'
    })
  },


  components: {
    MyHeader,
    MyGoods,
    MyFooter
  },
  // data(){
  //   return {
  //     // list: [] // 商品数据
  //   }
  // },
  // created(){
  //   this.$axios({
  //     url: "/api/cart"
  //   }).then(res => {
  //     console.log(res);
  //     this.list = res.data.list
  //   })
  // },
  created() {
    this.asyncSetGoodsList()
  },
  methods: {
    ...mapActions('cart', ['asyncSetGoodsList']),

    allFn(bool){
      this.list.forEach(obj => obj.goods_state = bool)
    }
  }
}
</script>

<style scoped>
  .main{
    padding-top: 45px;
    padding-bottom: 50px;
  }
</style>